﻿using Architecture.API.Application.Command.RequestForLeaveCommand;
using Architecture.Domain;
using Architecture.Domain.Dto.LeaveDto;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;

namespace Architecture.API.Controllers
{
    /// <summary>
    /// API控制器--请假条
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class LeaveManagementController : ControllerBase
    {
        /// <summary>
        /// 命令总线
        /// </summary>
        protected readonly IMediator mediator;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator">命令总线</param>
        public LeaveManagementController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 添加请假信息
        /// </summary>
        /// <param name="command">请假信息</param>
        /// <returns>返回受影响的行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddLeave(LeaveCreateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code= await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }


        /// <summary>
        /// 反填角色信息
        /// </summary>
        /// <param name="command">用户ID</param>
        /// <returns>反填角色信息</returns>
        [HttpPost]
        public async Task<ApiResult<UserInfo>> GetUserInfoLeave(UserReverseFillingCommand command)
        {
            ApiResult<UserInfo> result = new ApiResult<UserInfo>();
            try
            {
                result.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 通过部门ID获取用户信息
        /// </summary>
        /// <param name="command">请求的部门ID</param>
        /// <returns>返回部门信息</returns>
        [HttpPost]
        public async Task<ApiResult<List<UserInfoDepartmentDto>>> GetUserInfoByDepartmentId(UserInfoDepartmentCommand command)
        {
            ApiResult<List<UserInfoDepartmentDto>> result = new ApiResult<List<UserInfoDepartmentDto>>();
            try
            {
                result.Data= await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code=-1; 
                result.Message = ex.Message;
                throw;
            }
            return result;
        }


        /// <summary>
        /// 请假分页列表显示
        /// </summary>
        /// <param name="command">请求的条件</param>
        /// <returns>返回请假信息</returns>
        [HttpPost]
        public async Task<ApiResult<LeaveListDto<GetInfoDto>>> GetLeaveList(LeaveListSeachCommand command)
        {
            ApiResult<LeaveListDto<GetInfoDto>> result = new ApiResult<LeaveListDto<GetInfoDto>>();
            try
            {
                result.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 反填请假信息
        /// </summary>
        /// <param name="command">请求的条件</param>
        /// <returns>返回请假信息</returns>
        [HttpPost]
        public async Task<ApiResult<RequestForLeave>> ReverseLeaveInfo(LeaveReverseCommand command)
        {
            ApiResult<RequestForLeave> result = new ApiResult<RequestForLeave>();
            try
            {
                result.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 审批请假信息
        /// </summary>
        /// <param name="command">请假信息</param>
        /// <returns>返回受影响的行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> ToExamineLeaveInfo(ToExamineLeaveCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 修改请假信息
        /// </summary>
        /// <param name="command">请假信息</param>
        /// <returns>返回受影响的行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdateLeaveInfo(LeaveUpdateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 删除请假信息
        /// </summary>
        /// <param name="command">请假ID</param>
        /// <returns>返回受影响的行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> DeletedLeaveInfo(DeletedLeaveCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

    }
}
